package com.api.sw.duban.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.sw.duban.action.DubanGzAction;
import com.api.sw.duban.action.DubanJcAction;
import com.api.sw.duban.action.DubanqsAction;
import com.api.sw.duban.model.Assigned;
import com.api.sw.duban.service.AssignedBaseService;
import com.api.sw.duban.util.*;
import com.engine.common.util.ParamUtil;
import weaver.conn.RecordSet;
import weaver.docs.share.DocShareUtil;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.text.SimpleDateFormat;
import java.util.*;

/*
    督办公共接口
 */
@Path("/dubancore")
public class DbanBaseAction {
    private final static LogTool log = new LogTool("/log/duban/",false);
    /*
           文档赋权
     */
    @GET
    @Path("/setfileqx")
    @Produces({"text/plain"})
    public String setFileqx(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        String fileid=Util.null2String(var1.getParameter("fileid")).trim();//文件ID
        String userid=Util.null2String(var1.getParameter("userid")).trim();//附件ID
        String dw=Util.null2String(var1.getParameter("dw")).trim();//单位ID
        userid= Util.null2String(DubUtil.getDcryByReceiveunit(dw));
        log.writeLog("setFileqx--->"+fileid+" "+userid);
        if(!"".equals(fileid)&&!"".equals(userid)){
            String[] fileids=fileid.split(",");
            for(String str:fileids){
                str=Util.null2String(str).trim();
                if(!"".equals(str)){
                    int docuser= Util.getIntValue(FileUtil.getFileCreaterByDocid(str),0) ;
                    if(docuser>0){
                        User user=new User(docuser);
                        DocShareUtil.addDocShare(user,str,"1",userid,"0","1");
                    }
                }
            }
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
           文档赋权 根据交办记录ID
     */
    @GET
    @Path("/setfileqxbyjlid")
    @Produces({"text/plain"})
    public String setFileqxbyjlid(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        String fileid=Util.null2String(var1.getParameter("fileid")).trim();//文件I
        String jbjlid=Util.null2String(var1.getParameter("jbjlid")).trim();//交办记录ID

        log.writeLog("setFileqxbyjlid--->"+fileid+" "+jbjlid);
        if(!"".equals(fileid)&&!"".equals(jbjlid)){
            String userid=DubUtil.getXgryByJbjl(jbjlid);
            log.writeLog("setFileqxbyjlid--->"+userid);
            //String userid= Util.null2String(DubUtil.getDcryByReceiveunit(dw));
            String[] fileids=fileid.split(",");
            for(String str:fileids){
                str=Util.null2String(str).trim();
                if(!"".equals(str)){
                    int docuser= Util.getIntValue(FileUtil.getFileCreaterByDocid(str),0) ;
                    if(docuser>0){
                        User user=new User(docuser);
                        DocShareUtil.addDocShare(user,str,"1",userid,"0","1");
                    }
                }
            }
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        模块 数据 赋权  根据收文单位
     */
    @GET
    @Path("/setfileqx")
    @Produces({"text/plain"})
    public String setModeqx(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        ModeRightInfo mri=new ModeRightInfo();
        //mri.editModeDataShare();
        String rwid=Util.null2String(var1.getParameter("rwid")).trim();//任务ID
        String dw=Util.null2String(var1.getParameter("dw")).trim();//单位
        log.writeLog("setModeqx--->"+rwid+" "+dw);
        if(!"".equals(rwid)&&!"".equals(dw)){
            //获取主办单位的人员
            String receiveunitry= Util.null2String(DubUtil.getDcryByReceiveunit(dw));
            if(!"".equals(receiveunitry)){
                flag=DubUtil.updRy(rwid,receiveunitry);
                if(flag){
                    int user=DubUtil.getRwCreateId(rwid);
                    mri.editModeDataShare(user,Util.getIntValue(DubUtil.modeId,0),Util.getIntValue(rwid,0));
                    String[] receiveunitrys=receiveunitry.split(",");
                    for(String str:receiveunitrys){
                        //int user=Util.getIntValue(Util.null2String(str).trim(),0) ;

                        //if(user>0&&dataid>0){

                        //}
                    }
                }

            }
        }else{
            mess="参数异常";
            flag=false;
        }
        return JSONObject.toJSONString(json);
    }
    /*
        补发附件接口
     */
    @GET
    @Path("/setfilebf")
    @Produces({"text/plain"})
    public String setFilebf(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;

        String fileid=Util.null2String(var1.getParameter("fileid")).trim();//文件ID
        String rwid=Util.null2String(var1.getParameter("rwid")).trim();//任务ID
        String ckry=DubUtil.getRwCkry(rwid);
        log.writeLog(ckry+ " setFilebf--->"+fileid+" "+rwid);
        if(!"".equals(fileid)&&!"".equals(rwid)&&!"".equals(ckry)){
            String[] fileids=fileid.split(",");
            for(String str:fileids){
                str=Util.null2String(str).trim();
                if(!"".equals(str)){
                    int docuser= Util.getIntValue(FileUtil.getFileCreaterByDocid(str),0) ;
                    if(docuser>0){
                        User user=new User(docuser);
                        DocShareUtil.addDocShare(user,str,"1",ckry,"0","1");
                    }
                }
            }
            //更新 表单附件
            DubUtil.UpdRwFj(rwid,fileid);
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        补发时限接口
     */
    @GET
    @Path("/setfilesx")
    @Produces({"text/plain"})
    public String setFilesx(@Context HttpServletRequest var1, @Context HttpServletResponse var2){

        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        List<String> list=new ArrayList<String>();
        String jbjlid=Util.null2String(var1.getParameter("jbjlid")).trim();//交办记录ID
        int count=Util.getIntValue(Util.null2String(var1.getParameter("count")).trim(),0)  ;//时限数量
        if(count>0){
            for(int i=0;i<count;i++){
                String bfsx=Util.null2String(Util.null2String(var1.getParameter("bfsx"+i))).trim();
                if(!"".equals(bfsx)){
                    list.add(bfsx);
                }
            }
        }
        log.writeLog(jbjlid+ " setFilesx--->"+count+" ");
         DubanGzAction DubanGzAction=new DubanGzAction();
         mess=DubanGzAction.SetBfSx(jbjlid,list);
         if(!"".equals(mess)){
             flag=true;
         }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
         补发单位接口  也就是交办记录
     */
    @GET
    @Path("/setfiledw")
    @Produces({"text/plain"})
    public String setFiledw(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String key1=UUID.randomUUID()+"";
        List<Object> listSql=new ArrayList<Object>();
        String mess="";
        boolean flag=true;
        Assigned assigned=null;
        List<String> list=new ArrayList<String>();
        String rwid=Util.null2String(var1.getParameter("rwid")).trim();//任务ID
        String zbdw=Util.null2String(var1.getParameter("zbdw")).trim()  ;//主办单位
        String xbdw=Util.null2String(var1.getParameter("xbdw")).trim()  ;//协办单位
        String yqsm=Util.null2String(var1.getParameter("yqsm")).trim()  ;//要求说明
        String xsjs=Util.null2String(var1.getParameter("xsjs")).trim()  ;//限时接收
        String xsfk=Util.null2String(var1.getParameter("xsfk")).trim()  ;//限时反馈
        String newry="";
        log.writeLog(rwid+ " setFiledw--->"+zbdw+" "+xbdw+" "+yqsm+" "+xsjs+" "+xsfk);
        if(!"".equals(rwid)&&!"".equals(zbdw)&&!"".equals(xsjs)&&!"".equals(xsfk)){
            String createid=DubUtil.getRwCustId(rwid,"modedatacreater");
            String fileid=DubUtil.getRwCustId(rwid,"scfj");
            //获取已经有的主办单位
            String yzbdw=DubUtil.getRwZbdw(rwid);
            String yxbdw=DubUtil.getRwXbdw(rwid);

            String[] zbdws=zbdw.split(",");
            for(String dwstr:zbdws){


                if("".equals(dwstr)||(","+yzbdw+",").indexOf((","+dwstr+","))>=0){
                    continue;
                }
                dwstr=Util.null2String(dwstr).trim();
                String djr=Util.null2String(DubUtil.getDcryByReceiveunit(dwstr)).trim() ;
                String Xbdjr=Util.null2String(DubUtil.getDcryByReceiveunit(xbdw)).trim() ;
                if(!"".equals(djr)){
                    if(djr.endsWith(",")){
                        djr=djr.substring(0,djr.length()-1);
                    }
                    newry=newry+djr+",";
                }
                if(!"".equals(Xbdjr)){
                    if(Xbdjr.endsWith(",")){
                        Xbdjr=Xbdjr.substring(0,Xbdjr.length()-1);
                    }
                    newry=newry+Xbdjr+",";
                }
                assigned=new Assigned();
                assigned.setDbry(createid);
                assigned.setYq(yqsm);
                assigned.setSsrwid(rwid);
                assigned.setDw(Util.getIntValue(dwstr,0));
                assigned.setZxb("0");
                assigned.setDjr(djr);
                assigned.setZt(0);
                assigned.setJssx(xsjs);
                assigned.setFksx(xsfk);
                assigned.setXbdjr(Xbdjr);
                assigned.setModeuuid(BaseUtil.getUUid());
                assigned.setKey1(key1);
                listSql.add(assigned);
            }
            log.writeLog(listSql.toString());
            if(listSql.size()>0){
                AssignedBaseService assignedBaseService=new AssignedBaseService();
                mess=Util.null2String(assignedBaseService.getObjMapList(listSql,null)) ;
                if("".equals(mess)){
                    DubanqsAction dubanqsAction=new DubanqsAction();
                    mess=dubanqsAction.setSxTimeBybf(rwid,key1);
                }
            }
            if("".equals(mess)){
                //跟新单位  主办以及协办单位
                yxbdw=DubUtil.SetHb(yxbdw,xbdw);
                yzbdw=DubUtil.SetHb(yzbdw,zbdw);
                log.writeLog("yxbdw"+yxbdw+" xbdw"+xbdw);
                log.writeLog("yzbdw"+yzbdw+" zbdw"+zbdw);
                DubUtil.updDw(rwid,yzbdw,yxbdw);

                //跟新数据  查看人员  附件 模块权限
                DubUtil.updRy(rwid,newry);
                DubUtil.GxFj(fileid,newry);//附件赋权
                //模块赋权
                ModeRightInfo mri=new ModeRightInfo();
                mri.editModeDataShare(Util.getIntValue(createid,0),Util.getIntValue(DubUtil.modeId,0),Util.getIntValue(rwid,0));
            }


        }else{
            mess="参数异常";
            flag=false;
        }
        if(!"".equals(mess)){
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        驳回 反馈内容
     */
    @GET
    @Path("/rejectFeedback")
    @Produces({"text/plain"})
    public String rejectFeedback(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        List<Map<String,Object>>  sqlList=new ArrayList<Map<String,Object>>();
        String mess="";
        boolean flag=true;
        String fkjlid=Util.null2String(var1.getParameter("fkjlid")).trim();//反馈记录ID
        String fksx=Util.null2String(var1.getParameter("fksx")).trim();//新反馈时限

        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        if(!"".equals(fkjlid)){
            Map<String,Object> map1=new HashMap<String,Object>();
            String sql="update uf_rwfkb set zt=? where id="+fkjlid;
            Object[] obj1=new Object[1];
            obj1[0]=1;
            map1.put("sql",sql);
            map1.put("params",obj1);
            sqlList.add(map1);
            if(!"".equals(fksx)){
                Map<String,Object> map2=new HashMap<String,Object>();
                String sql1="update uf_fksxjlbnew set fkyqsj=? where id in (select fksxjlid form  uf_rwfkb where id='"+fkjlid+"')";
                Object[] obj12=new Object[1];
                obj1[0]=fksx;
                map2.put("sql",sql1);
                map2.put("params",obj12);
                sqlList.add(map2);
            }
            if(sqlList.size()>0){
                SqlUtil sqlUtil=new SqlUtil();
                flag=sqlUtil.executeUpdateListTranRec(sqlList);
            }else{
                mess="参数异常";
                flag=false;
            }
        }else{
            mess="参数异常";
            flag=false;
        }

        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        跟新 时限时间
     */
    @GET
    @Path("/updatesx")
    @Produces({"text/plain"})
    public String updatesx(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        String fkjlid=Util.null2String(var1.getParameter("fkjlid")).trim();//反馈记录ID
        String newsx=Util.null2String(var1.getParameter("fksx")).trim();//反馈时限
        if(!"".equals(fkjlid)&&!"".equals(newsx)){
            RecordSet re=new RecordSet();
            String sql="update uf_fksxjlbnew  set fkyqsj='"+newsx+"'  where id="+fkjlid;
            re.execute(sql);
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        审批申请
     */

    @GET
    @Path("/shenpisq")
    @Produces({"text/plain"})
    public String shenpisq(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String mess="";
        boolean flag=true;
        String shenqid=Util.null2String(var1.getParameter("shenqid")).trim();//申请记录ID

        if(!"".equals(shenqid)){
            RecordSet re=new RecordSet();
            String sql="update uf_sqjlb  set zt='1'  where id="+shenqid;
            re.execute(sql);
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    @GET
    @Path("/text")
    @Produces({"text/plain"})
    public String text(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        json.put("flag",true);
        json.put("mess","测试地址");
        return JSONObject.toJSONString(json);
    }
    /*
        办结主办单位任务
     */
    @GET
    @Path("/dubanbj")
    @Produces({"text/plain"})
    public String dubanbj(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String mess="";
        boolean flag=true;
        String jbjlid=Util.null2String(var1.getParameter("jbjl")).trim();//交办记录ID

        if(!"".equals(jbjlid)){
            if(jbjlid.endsWith(",")){
                jbjlid=jbjlid.substring(0,jbjlid.length()-1);
            }
            RecordSet re=new RecordSet();
            String sql="update uf_dbjblb  set zt='2',bjsj='"+(sim.format(new Date()))+"'  where id in ("+jbjlid+")";
            flag=re.execute(sql);
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        取消办结
     */
    @GET
    @Path("/dubanbjqx")
    @Produces({"text/plain"})
    public String dubanbjqx(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String mess="";
        boolean flag=true;
        String jbjlid=Util.null2String(var1.getParameter("jbjl")).trim();//交办记录ID

        if(!"".equals(jbjlid)){
            if(jbjlid.endsWith(",")){
                jbjlid=jbjlid.substring(0,jbjlid.length()-1);
            }
            RecordSet re=new RecordSet();
            String sql="update uf_dbjblb  set zt='1',bjsj='"+(sim.format(new Date()))+"'  where id in ("+jbjlid+") and zt=2";
            flag=re.execute(sql);
        }else{
            mess="参数异常";
            flag=false;
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        获取所有 通知下的反馈记录的附件 包含 限时 以及 定期 补充
     */
    @GET
    @Path("/dubanfiles")
    @Produces({"text/plain"})
    public String dubanfiles(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String mess="";
        boolean flag=true;
        String tzid=Util.null2String(var1.getParameter("tzid")).trim();//通知ID
        if(!"".equals(tzid)){
                if(tzid.endsWith(",")){
                tzid=tzid.substring(0,tzid.length()-1);
            }
            RecordSet re=new RecordSet();
            String sql="select b.fkfj from uf_dbjblb a,uf_rwfkb b where ssrwid="+tzid+" and a.id=b.jbjlid and isnull(b.zt,-1) in (-1,0)";
            re.execute(sql);
            while(re.next()){
                String fkfj=Util.null2String(re.getString("fkfj")).trim();
                if("".equals(fkfj)){
                    continue;
                }
                if(fkfj.endsWith(",")){
                    fkfj=fkfj.substring(0,fkfj.length()-1);
                }
                mess=mess+fkfj+",";
            }
        }else{
            mess="参数异常";
            flag=false;
        }
        if(mess.endsWith(",")){
            mess=mess.substring(0,mess.length()-1);
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return JSONObject.toJSONString(json);
    }
    /*
        催办
     */
    @POST
    @Path("/urging")
    @Produces({"text/plain"})
    public String Urging(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        System.out.println("de");
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(var1, var2);
            DubanqsAction DubanqsAction=new DubanqsAction();
            apidatas=DubanqsAction.Urging(ParamUtil.request2Map(var1),user);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flah", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    @POST
    @Path("/getimagefile")
    @Produces({"text/plain"})
    public String getImagefile(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String docid=Util.null2String(var1.getParameter("docid"));
        String imagefileid="";
        System.out.println("de");
        try {
            RecordSet re=new RecordSet();
            String sql="select imagefileid from docimagefile where docid="+docid;
            re.execute(sql);
            if(re.next()){
                imagefileid=Util.null2String(re.getString("imagefileid"));
            }
            apidatas.put("imagefileid",imagefileid);
            apidatas.put("url","/weaver/weaver.file.FileDownload?fileid="+imagefileid+"&nolog=1");

            apidatas.put("flag",true);
            log.writeLog(apidatas.toString());
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flag", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
    @POST
    @Path("/getdocid")
    @Produces({"text/plain"})
    public String getDocidBybill(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String billid=Util.null2String(var1.getParameter("billid"));
        String docid="";
        System.out.println("de");
        try {
            RecordSet re=new RecordSet();
            String sql="select zw from uf_dcsxzb where id="+billid+"" +
                       " union all " +
                       "select zw from uf_jcdctz where id="+billid+"" ;
            re.execute(sql);
            if(re.next()){
                docid=Util.null2String(re.getString("zw"));
            }
            apidatas.put("docid",docid);
            apidatas.put("flag",true);
            log.writeLog(apidatas.toString());
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("flag", false);
            apidatas.put("flag", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
}
